﻿Imports System.Data.SqlClient
Imports CrystalCommon.BLL
Imports System.Data.DataView
Imports System.Data
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Crystal.CLL

Public Class cAddEditLKPTable
#Region "Declare"
    Public context As CrystalEntities
#End Region
#Region "Database Functions"
    ''' <summary>
    '''  'To Select Reference Table
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function LoadReferenceTable() As ExecutionResult
        Dim eReturnVal As New ExecutionResult
        eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_MSTR_RefrenceTables", CommandType.StoredProcedure, ReturnTypes.DataSet)
        Return eReturnVal
    End Function

    Public Function LoadReferenceTableData(ByVal TableID As Integer) As ExecutionResult
        Dim eReturnVal As New ExecutionResult
        Dim param(0) As SqlParameter
        param(0) = New SqlParameter("@TableID", TableID)
        eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_MSTR_RefrenceTableData", CommandType.StoredProcedure, ReturnTypes.DataSet, param)
        Return eReturnVal
    End Function

    ''' <summary>
    ''' before going to delete/deit the record it checks whether teh record exist
    ''' </summary>
    ''' <param name="tablename"></param>
    ''' <param name="id"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CheckRecordExistence(ByVal TableID As Integer, ByVal ID As Integer) As ExecutionResult
        Dim eReturnVal As New ExecutionResult
        Dim param(1) As SqlParameter
        param(0) = New SqlParameter("@TableID", TableID)
        param(1) = New SqlParameter("@ID", ID)
        eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_MSTR_CheckRecordExistence", CommandType.StoredProcedure, ReturnTypes.DataSet, param)
        Return eReturnVal
    End Function

    ''' <summary>
    ''' checks the reference of the record before deleting
    ''' </summary>
    ''' <param name="tablename"></param>
    ''' <param name="id"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CheckBeforeDelete(ByVal TableID As Integer, ByVal ID As Integer) As Boolean
        Dim eReturnVal As New ExecutionResult
        Dim strResult As String = String.Empty
        Dim param(1) As SqlParameter
        param(0) = New SqlParameter("@TableID", TableID)
        param(1) = New SqlParameter("@ID", ID)
        eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_MSTR_CheckBeforeDelete", CommandType.StoredProcedure, ReturnTypes.Scalar, param)
        strResult = CType(eReturnVal.ResultObject, String)
        If (strResult = "False") Then
            Return False
        Else
            Return True
        End If
    End Function

    ''' <summary>
    '''  'To Select Reference Table Column Info
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SelectReferenceTableColumnInfo(ByVal TableID As Integer) As ExecutionResult
        Dim eReturnVal As New ExecutionResult
        Dim param(0) As SqlClient.SqlParameter
        param(0) = New SqlParameter("@TableID", TableID)
        eReturnVal = CoreFunctions.Execute(strConfiguration, "CP_SEL_ReferenceTableInfo", CommandType.StoredProcedure, ReturnTypes.DataSet, param)
        Return eReturnVal
    End Function

    ''' <summary>
    '''  'To Insert/Update/Delete Reference Table Column Info
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function InsUpdDelReferenceTable(ByVal strAction As String, ByVal strTableName As String, ByVal strPart1 As String, ByVal strPart2 As String, Optional ByRef autoid As String = "0") As Boolean
        Dim storedprocedurename As String = "CP_INS_UPD_Reference_Table"
        Dim cmd As SqlCommand = Nothing
        Dim param(0) As SqlClient.SqlParameter
        param(0) = New SqlParameter("@ACTION", strAction)
        param(1) = New SqlParameter("@strAction", strAction)
        param(2) = New SqlParameter("@strTableName", strTableName)
        param(3) = New SqlParameter("@strPart1", strPart1)
        param(4) = New SqlParameter("@strPart2", strPart2)
        Return True
    End Function

    ''' <summary>
    ''' checks the reference of the record before deleting
    ''' </summary>
    ''' <param name="tablename"></param>
    ''' <param name="id"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DeleteReferenceTableData(ByVal TableID As Integer, ByVal ID As Integer) As Boolean
        Dim eReturnVal As New ExecutionResult
        Dim strResult As String = String.Empty

        Dim param(1) As SqlParameter
        param(0) = New SqlParameter("@TableID", TableID)
        param(1) = New SqlParameter("@ID", ID)
        eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_MSTR_DeleteRefTableData", CommandType.StoredProcedure, ReturnTypes.Scalar, param)
        strResult = CType(eReturnVal.ResultObject, String)
        If (strResult = "True") Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function LoadAllCountries() As ExecutionResult
        Dim eReturnVal As New ExecutionResult
        eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_GetAllCountries", CommandType.StoredProcedure, ReturnTypes.DataSet)
        Return eReturnVal
    End Function

    Public Function LoadAllMasterType() As ExecutionResult
        Dim eReturnVal As New ExecutionResult
        eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_GetAllMasterType", CommandType.StoredProcedure, ReturnTypes.DataSet)
        Return eReturnVal
    End Function
    Public Function LoadAllInvestorType() As ExecutionResult
        Dim eReturnVal As New ExecutionResult
        eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_GetAllInvestorType", CommandType.StoredProcedure, ReturnTypes.DataSet)
        Return eReturnVal
    End Function
    Public Function LoadAllRegions() As ExecutionResult
        Dim eReturnVal As New ExecutionResult
        eReturnVal = CoreFunctions.Execute(strConfiguration, "CR_sp_GetAllRegions", CommandType.StoredProcedure, ReturnTypes.DataSet)
        Return eReturnVal
    End Function

    Public Function CheckRecordDataExist(ByVal tablename As String, ByVal id As Integer, ByVal firstparameter As String, Optional ByVal secondparameter As String = "", Optional ByVal thirdparameter As String = "") As String
        Dim var As String = String.Empty
        context = New CrystalEntities(CommonCLL.mstrContextConnectionstring)
        Select Case tablename.ToLower.ToString().Trim()
            Case "activist"
                If id = 0 Then
                    var = (From p In context.LKP_Activist() _
                           Where p.ActivistCategory = firstparameter Select p.ActivistCategory).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_Activist() _
                           Where p.ActivistCategory = firstparameter And p.ActivistID <> id Select p.ActivistCategory).DefaultIfEmpty(String.Empty).First()
                End If
                If var = String.Empty Then
                    If id = 0 Then
                        var = (From p In context.LKP_Activist() _
                               Where p.ActivistCode = secondparameter Select p.ActivistCode).DefaultIfEmpty(String.Empty).First()
                    Else
                        var = (From p In context.LKP_Activist() _
                               Where p.ActivistCode = secondparameter And p.ActivistID <> id Select p.ActivistCode).DefaultIfEmpty(String.Empty).First()
                    End If
                    If var <> String.Empty Then
                        Return var
                    End If
                Else
                    Return var
                End If

            Case "city"
                If id = 0 Then
                    var = (From p In context.LKP_City() _
                           Where p.City = firstparameter Select p.City).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_City() _
                           Where p.City = firstparameter And p.CityID <> id Select p.City).DefaultIfEmpty(String.Empty).First()
                End If
                If var <> String.Empty Then
                    Return var
                End If

            Case "country"
                If id = 0 Then
                    var = (From p In context.LKP_Country() _
                           Where p.CountryCode = firstparameter Select p.CountryCode).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_Country() _
                           Where p.CountryCode = firstparameter And p.CountryID <> id Select p.CountryCode).DefaultIfEmpty(String.Empty).First()
                End If
                If var = String.Empty Then
                    If id = 0 Then
                        var = (From p In context.LKP_Country() _
                               Where p.CountryName = secondparameter Select p.CountryName).DefaultIfEmpty(String.Empty).First()
                    Else
                        var = (From p In context.LKP_Country() _
                               Where p.CountryName = secondparameter And p.CountryID <> id Select p.CountryName).DefaultIfEmpty(String.Empty).First()
                    End If
                    If var <> String.Empty Then
                        Return var
                    End If
                Else
                    Return var
                End If

            Case "exchange"
                If id = 0 Then
                    var = (From p In context.LKP_Exchange() _
                           Where p.Exchange = firstparameter Select p.Exchange).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_Exchange() _
                           Where p.Exchange = firstparameter And p.ExchangeID <> id Select p.Exchange).DefaultIfEmpty(String.Empty).First()
                End If
                If var <> String.Empty Then
                    Return var
                End If

            Case "fund type"
                If id = 0 Then
                    var = (From p In context.LKP_FundType() _
                           Where p.FundType = firstparameter Select p.FundType).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_FundType() _
                           Where p.FundType = firstparameter And p.FundTypeID <> id Select p.FundType).DefaultIfEmpty(String.Empty).First()
                End If
                If var = String.Empty Then
                    If id = 0 Then
                        var = (From p In context.LKP_FundType() _
                               Where p.FundTypeDescription = secondparameter Select p.FundTypeDescription).DefaultIfEmpty(String.Empty).First()
                    Else
                        var = (From p In context.LKP_FundType() _
                               Where p.FundTypeDescription = secondparameter And p.FundTypeID <> id Select p.FundTypeDescription).DefaultIfEmpty(String.Empty).First()
                    End If
                    If var <> String.Empty Then
                        Return var
                    End If
                Else
                    Return var
                End If

            Case "head investor"
                If id = 0 Then
                    var = (From p In context.LKP_HeadInvestor() _
                           Where p.HeadInvestor = firstparameter Select p.HeadInvestor).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_HeadInvestor() _
                           Where p.HeadInvestor = firstparameter And p.HeadInvestorID <> id Select p.HeadInvestor).DefaultIfEmpty(String.Empty).First()
                End If
                If var <> String.Empty Then
                    Return var
                End If

            Case "industry"
                If id = 0 Then
                    var = (From p In context.LKP_Industry() _
                           Where p.Industry = firstparameter Select p.Industry).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_Industry() _
                           Where p.Industry = firstparameter And p.IndustryID <> id Select p.Industry).DefaultIfEmpty(String.Empty).First()
                End If
                If var <> String.Empty Then
                    Return var
                End If

            Case "investor type"
                If id = 0 Then
                    var = (From p In context.LKP_InvestorType() _
                           Where p.InvestorType = firstparameter Select p.InvestorType).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_InvestorType() _
                           Where p.InvestorType = firstparameter And p.InvestorTypeID <> id Select p.InvestorType).DefaultIfEmpty(String.Empty).First()
                End If
                If var <> String.Empty Then
                    Return var
                End If

            Case "metro"
                If id = 0 Then
                    var = (From p In context.LKP_Metro() _
                           Where p.Metro = firstparameter Select p.Metro).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_Metro() _
                           Where p.Metro = firstparameter And p.MetroId <> id Select p.Metro).DefaultIfEmpty(String.Empty).First()
                End If
                If var <> String.Empty Then
                    Return var
                End If

            Case "region"
                If id = 0 Then
                    var = (From p In context.LKP_Region() _
                           Where p.RegionCode = firstparameter Select p.RegionCode).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_Region() _
                           Where p.RegionCode = firstparameter And p.RegionID <> id Select p.RegionCode).DefaultIfEmpty(String.Empty).First()
                End If
                If var = String.Empty Then
                    If id = 0 Then
                        var = (From p In context.LKP_Region() _
                               Where p.Region = secondparameter Select p.Region).DefaultIfEmpty(String.Empty).First()
                    Else
                        var = (From p In context.LKP_Region() _
                               Where p.Region = secondparameter And p.RegionID <> id Select p.Region).DefaultIfEmpty(String.Empty).First()
                    End If
                    If var <> String.Empty Then
                        Return var
                    End If
                Else
                    Return var
                End If

            Case "sector"
                If id = 0 Then
                    var = (From p In context.LKP_Sector() _
                           Where p.Sector = firstparameter Select p.Sector).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_Sector() _
                           Where p.Sector = firstparameter And p.SectorID <> id Select p.Sector).DefaultIfEmpty(String.Empty).First()
                End If
                If var <> String.Empty Then
                    Return var
                End If

            Case "state"
                If id = 0 Then
                    var = (From p In context.LKP_State() _
                           Where p.StateCode = firstparameter Select p.StateCode).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_State() _
                           Where p.StateCode = firstparameter And p.StateID <> id Select p.StateCode).DefaultIfEmpty(String.Empty).First()
                End If
                If var = String.Empty Then
                    If id = 0 Then
                        var = (From p In context.LKP_State() _
                               Where p.State = secondparameter Select p.State).DefaultIfEmpty(String.Empty).First()
                    Else
                        var = (From p In context.LKP_State() _
                               Where p.State = secondparameter And p.StateID <> id Select p.State).DefaultIfEmpty(String.Empty).First()
                    End If
                    If var <> String.Empty Then
                        Return var
                    End If
                Else
                    Return var
                End If

            Case "style"
                If id = 0 Then
                    var = (From p In context.LKP_Style() _
                           Where p.Style = firstparameter Select p.Style).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_Style() _
                           Where p.Style = firstparameter And p.StyleID <> id Select p.Style).DefaultIfEmpty(String.Empty).First()
                End If
                If var <> String.Empty Then
                    Return var
                End If

            Case "territory"
                If id = 0 Then
                    var = (From p In context.LKP_Territory() _
                           Where p.Territory = firstparameter Select p.Territory).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_Territory() _
                           Where p.Territory = firstparameter And p.TerritoryID <> id Select p.Territory).DefaultIfEmpty(String.Empty).First()
                End If
                If var <> String.Empty Then
                    Return var
                End If

            Case "ultimate parent"
                If id = 0 Then
                    var = (From p In context.LKP_UltimateParent() _
                           Where p.UltimateParent = firstparameter Select p.UltimateParent).DefaultIfEmpty(String.Empty).First()
                Else
                    var = (From p In context.LKP_UltimateParent() _
                           Where p.UltimateParent = firstparameter And p.UltimateParentID <> id Select p.UltimateParent).DefaultIfEmpty(String.Empty).First()
                End If
                If var <> String.Empty Then
                    Return var
                End If
        End Select
        Return var
    End Function

    Public Function GetCities(ByVal cityText As String) As DataSet
        Dim obj As ExecutionResult = Nothing
        Dim dSet As New DataSet
        Dim eReturnVal As New ExecutionResult
        Dim param(0) As SqlParameter
        param(0) = New SqlParameter("@Name", cityText)
        obj = CoreFunctions.Execute(strConfiguration, "CR_sp_MSTR_LoadCities", CommandType.StoredProcedure, ReturnTypes.DataSet, param)
        dSet = CType(obj.ResultObject, DataSet)
        Return dSet
    End Function
    Public Function AddNewRecords(ByVal tableID As Integer, ByVal tableData As String) As DataSet
        Dim obj As ExecutionResult = Nothing
        Dim dSet As New DataSet
        Dim eReturnVal As New ExecutionResult
        Dim param(1) As SqlParameter
        param(0) = New SqlParameter("@TableID", tableID)
        param(1) = New SqlParameter("@TableData", tableData)
        obj = CoreFunctions.Execute(strConfiguration, "CR_sp_MTB_InsertRecords", CommandType.StoredProcedure, ReturnTypes.DataSet, param)
        dSet = CType(obj.ResultObject, DataSet)
        Return dSet
    End Function
    Public Function UpdateRecords(ByVal tableID As Integer, ByVal tableData As String) As DataSet
        Dim obj As ExecutionResult = Nothing
        Dim dSet As New DataSet
        Dim eReturnVal As New ExecutionResult
        Dim param(1) As SqlParameter
        param(0) = New SqlParameter("@TableID", tableID)
        param(1) = New SqlParameter("@TableData", tableData)
        obj = CoreFunctions.Execute(strConfiguration, "CR_sp_MTB_UpdateRecords", CommandType.StoredProcedure, ReturnTypes.DataSet, param)
        dSet = CType(obj.ResultObject, DataSet)
        Return dSet
    End Function
    Public Function Donotupdateifchangesnotmade(ByVal tablename As String, ByVal id As Integer, ByVal firstparameter As String, Optional ByVal secondparameter As String = "", Optional ByVal thirdparameter As String = "") As Integer
        Dim totalrecords As Integer = 0
        context = New CrystalEntities(CommonCLL.mstrContextConnectionstring)
        Select Case tablename.ToLower.ToString().Trim()
            Case "activist"
                totalrecords = (From p In context.LKP_Activist() _
                           Where p.ActivistID = id And p.ActivistCategory = firstparameter And p.ActivistCode = secondparameter Select p).Count
            Case "city"
                totalrecords = (From p In context.LKP_City() _
                            Where p.CityID = id And p.City = firstparameter And p.CountryID = secondparameter Select p).Count

            Case "country"
                totalrecords = (From p In context.LKP_Country() _
                           Where p.CountryID = id And p.CountryCode = firstparameter And p.CountryName = secondparameter And p.RegionID = thirdparameter Select p).Count

            Case "exchange"
                totalrecords = (From p In context.LKP_Exchange() _
                           Where p.ExchangeID = id And p.Exchange = firstparameter Select p).Count

            Case "fund type"
                totalrecords = (From p In context.LKP_FundType() _
                           Where p.FundTypeID = id And p.FundType = firstparameter And p.FundTypeDescription = secondparameter Select p).Count

            Case "head investor"
                totalrecords = (From p In context.LKP_HeadInvestor() _
                           Where p.HeadInvestorID = id And p.HeadInvestor = firstparameter Select p).Count

            Case "industry"
                totalrecords = (From p In context.LKP_Industry() _
                           Where p.IndustryID = id And p.Industry = firstparameter Select p).Count

            Case "investor type"
                ' Dim value As Integer = Convert.ToInt32(thirdparameter)
                totalrecords = (From p In context.LKP_InvestorType() _
                           Where p.InvestorTypeID = id And p.InvestorType = firstparameter And p.InvestorTypeCode = secondparameter And p.IsExcludedFromSearch = thirdparameter
                           Select p).Count

            Case "metro"
                totalrecords = (From p In context.LKP_Metro() _
                           Where p.MetroId = id And p.Metro = firstparameter Select p).Count

            Case "region"
                totalrecords = (From p In context.LKP_Region() _
                           Where p.RegionID = id And p.RegionCode = firstparameter And p.Region = secondparameter Select p).Count

            Case "sector"
                totalrecords = (From p In context.LKP_Sector() _
                           Where p.SectorID = id And p.Sector = firstparameter Select p).Count

            Case "state"
                totalrecords = (From p In context.LKP_State() _
                           Where p.StateID = id And p.StateCode = firstparameter And p.State = secondparameter And p.CountryID = thirdparameter Select p).Count

            Case "style"
                totalrecords = (From p In context.LKP_Style() _
                           Where p.StyleID = id And p.Style = firstparameter Select p).Count

            Case "territory"
                totalrecords = (From p In context.LKP_Territory() _
                           Where p.TerritoryID = id And p.Territory = firstparameter Select p).Count

            Case "ultimate parent"
                totalrecords = (From p In context.LKP_UltimateParent() _
                           Where p.UltimateParentID = id And p.UltimateParent = firstparameter Select p).Count

            Case "master investor type"
                totalrecords = (From p In context.LKP_MasterInvestorType() _
                           Where p.MInvestorTypeID = id And p.MInvestorType = firstparameter And p.MInvestorTypeCode = secondparameter Select p).Count

            Case "turnover"
                totalrecords = (From p In context.LKP_Turnover() _
                           Where p.TurnoverID = id And p.Turnover = firstparameter Select p).Count

            Case "investor type linking"
                totalrecords = (From p In context.LKP_TypeMTypeLinking() _
                           Where p.TypeMTypeLinkID = id And p.InvestorTypeID = firstparameter And p.MInvestorTypeID = secondparameter Select p).Count
        End Select
        Return totalrecords
    End Function

    Public Function AddUpdateRecord(ByVal tablename As String, ByRef Sid As String, ByVal firstparameter As String, Optional ByVal secondparameter As String = "", Optional ByVal thirdparameter As String = "") As Boolean
        Dim ValidID As Integer = 0
        Dim id As Integer = 0
        If Sid <> String.Empty Then
            id = Convert.ToInt16(Sid)
        End If
        Dim newID As Integer = id
        context = New CrystalEntities(CommonCLL.mstrContextConnectionstring)
        Dim manager As EntityTypeManager = New EntityTypeManager()
        Select Case tablename.ToLower.ToString().Trim()
            Case "activist"
                'Dim activist As LKP_Activist
                If id <> 0 Then
                    ValidID = (From c In context.LKP_Activist() _
                                Where c.ActivistCategory = firstparameter And c.ActivistCode = secondparameter And c.ActivistID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_Activist() _
                                Where c.ActivistCategory = firstparameter And c.ActivistCode = secondparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If
            Case "city"
                'Dim city As LKP_City
                If id <> 0 Then
                    ValidID = (From c In context.LKP_City() _
                                Where c.City = firstparameter And c.CountryID = secondparameter And c.CityID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_City() _
                                Where c.City = firstparameter And c.CountryID = secondparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If
            Case "country"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_Country() _
                                Where c.CountryCode = firstparameter And c.CountryName = secondparameter And c.RegionID = thirdparameter And c.CountryID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_Country() _
                                Where c.CountryCode = firstparameter And c.CountryName = secondparameter And c.RegionID = thirdparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "exchange"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_Exchange() _
                                Where c.Exchange = firstparameter And c.ExchangeID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_Exchange() _
              Where c.Exchange = firstparameter
              Select c).Count

                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "fund type"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_FundType() _
                                Where c.FundType = firstparameter And c.FundTypeDescription = secondparameter And c.FundTypeID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_FundType() _
                                Where c.FundType = firstparameter And c.FundTypeDescription = secondparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "head investor"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_HeadInvestor() _
                                Where c.HeadInvestor = firstparameter And c.HeadInvestorID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_HeadInvestor() _
                                Where c.HeadInvestor = firstparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "industry"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_Industry() _
                                Where c.Industry = firstparameter And c.IndustryID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_Industry() _
                                Where c.Industry = firstparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "investor type"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_InvestorType() _
                                Where c.InvestorType = firstparameter And c.InvestorTypeID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_InvestorType() _
                                Where c.InvestorType = firstparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "metro"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_Metro() _
                                Where c.Metro = firstparameter And c.MetroId <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_Metro() _
                                Where c.Metro = firstparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "region"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_Region() _
                                Where c.Region = firstparameter And c.RegionCode = firstparameter And c.RegionID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_Region() _
                                Where c.Region = firstparameter And c.RegionCode = firstparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "sector"
                If id <> 0 Then

                    ValidID = (From c In context.LKP_Sector() _
                                Where c.Sector = firstparameter And c.SectorID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_Sector() _
                                Where c.Sector = firstparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "state"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_State() _
                                Where c.State = firstparameter And c.StateCode = secondparameter And c.CountryID = thirdparameter And c.StateID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_State() _
                                Where c.State = firstparameter And c.StateCode = secondparameter And c.CountryID = thirdparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "style"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_Style() _
                                Where c.Style = firstparameter And c.StyleID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_Style() _
                                Where c.Style = firstparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "territory"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_Territory() _
                                Where c.Territory = firstparameter And c.TerritoryID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_Territory() _
                                Where c.Territory = firstparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

            Case "ultimate parent"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_UltimateParent() _
                                Where c.UltimateParent = firstparameter And c.UltimateParentID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_UltimateParent() _
                                Where c.UltimateParent = firstparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If
            Case "turnover"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_Turnover() _
                                Where c.Turnover = firstparameter And c.TurnoverID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_Turnover() _
                                Where c.Turnover = firstparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If
            Case "investor type linking"
                If id <> 0 Then
                    ValidID = (From c In context.LKP_TypeMTypeLinking() _
                                Where c.InvestorTypeID = firstparameter And c.MInvestorTypeID = secondparameter And c.TypeMTypeLinkID <> newID
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                Else
                    ValidID = (From c In context.LKP_TypeMTypeLinking() _
                                Where c.InvestorTypeID = firstparameter And c.MInvestorTypeID = secondparameter
                                Select c).Count
                    If ValidID = 0 Then
                        Return True
                    Else
                        Return False
                    End If
                End If

        End Select
        Return True
    End Function


#End Region
End Class
